package com.crazypos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crazypos.constant.PageConstant;
import com.crazypos.dao.CategoryDao;
import com.crazypos.dao.OrganizationDao;
import com.crazypos.enums.YesOrNo;
import com.crazypos.exceptions.MyException;
import com.crazypos.pojo.CategoryEntity;
import com.crazypos.service.ICategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class CategoreService extends ServiceImpl<CategoryDao, CategoryEntity> implements ICategoryService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private OrganizationDao organizationDao;

    @Override
    @Transactional
    public void createCategory(boolean isAdmin, String name, String organizationId, String createdBy) {
        CategoryEntity entity = new CategoryEntity();
        entity.setName(name);
        entity.setSlug(name.toLowerCase());
        entity.setCreatedBy(createdBy);
        if (isAdmin) {
            entity.setIsSystem(YesOrNo.YES.getValue());
            entity.setOrganizationId("0");
        } else {
            entity.setIsSystem(YesOrNo.NO.getValue());
            int result = organizationDao.checkUserOrgIsCorrect(createdBy, organizationId);
            if (result < 1) {
                throw new MyException(500, "不存在此机构");
            }
            entity.setOrganizationId(organizationId);
        }

        categoryDao.insert(entity);
    }

    @Override
    @Transactional
    public void updateCategory(boolean isAdmin, String categoryId, String name, String organizationId) {
        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", categoryId);
        if (!isAdmin) {
            queryWrapper.eq("organizationId", organizationId);
        }

        int result = categoryDao.selectCount(queryWrapper);
        if (result < 1) {
            throw new MyException(500, "不存在该分类");
        }

        CategoryEntity entity = new CategoryEntity();
        entity.setId(categoryId);
        entity.setName(name);
        entity.setSlug(name.toLowerCase());

        categoryDao.updateById(entity);
    }

    @Override
    public IPage<CategoryEntity> adminGetCategoryList(Integer startPage, Integer pageSize, String keyword, Integer isSystem) {
        if (startPage == null || startPage <= 0) {
            startPage = PageConstant.START_PAGE;
        }

        if (pageSize == null || pageSize <= 0) {
            pageSize = PageConstant.PAGE_SIZE;
        }

        QueryWrapper<CategoryEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_system", YesOrNo.YES.getValue());

        if (keyword != null && !StringUtils.isEmpty(keyword.trim())) {
            queryWrapper.like("name", keyword.trim());
        }

        Page<CategoryEntity> page = new Page<>(startPage, pageSize);


        return categoryDao.selectPage(page, queryWrapper);
    }

    @Override
    public int getCountByOrzId(String categoryId, String organizationId) {
        return categoryDao.selectCount(
                new QueryWrapper<CategoryEntity>()
                        .eq("id", categoryId)
                        .eq("organization_id", organizationId)
        );
    }
}
